package ru.amse.tsyganov.jumleditor.graphicaleditor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;

import ru.amse.tsyganov.jumleditor.commands.Command;
import ru.amse.tsyganov.jumleditor.commands.CommandFactory;
import ru.amse.tsyganov.jumleditor.model.common.State;
import ru.amse.tsyganov.jumleditor.view.TrackingRectangle;
import ru.amse.tsyganov.jumleditor.view.common.StateView;
import ru.amse.tsyganov.jumleditor.view.preferences.Colors;
import ru.amse.tsyganov.jumleditor.view.preferences.Cursors;

/*package*/ class TrackingStateMode extends EditorMode {

	private List<TrackingRectangle> trackingRectangles = 
		new ArrayList<TrackingRectangle>();
	
	private final GraphicalEditor graphicalEditor;
	
	public TrackingStateMode(GraphicalEditor graphicalEditor, int x, int y) {
		
		if (graphicalEditor == null) {
			throw new IllegalArgumentException();
		}		
		this.graphicalEditor = graphicalEditor;
		
		for (StateView<? extends State> v : 
			graphicalEditor.getActivityGraphView().
				getSelectedItems().getSelectedStates()) {
			
			TrackingRectangle r = new TrackingRectangle(v);
			r.setDx(x - r.getRectangle().x);
			r.setDy(y - r.getRectangle().y);
			trackingRectangles.add(r);
		}	
	}
	
	@Override
	public void initState() {
		graphicalEditor.getActivityGraphView().setPaintConnectionPoints(false);
		graphicalEditor.getCanvas().setCursor(Cursors.getCursorSizeAll());
		graphicalEditor.getCanvas().redraw();
	}
	
	@Override
	public void keyPressed(KeyEvent e) {
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void mouseDoubleClick(MouseEvent e) {
	}

	@Override
	public void mouseDown(MouseEvent e) {
	}

	@Override
	public void mouseMove(MouseEvent e) {
		if (trackingRectangles.size() > 0) {	
			for (TrackingRectangle r : trackingRectangles) {
				r.getRectangle().x = e.x - r.getDx();
				r.getRectangle().y = e.y - r.getDy();
			}
		}
	}

	@Override
	public void mouseUp(MouseEvent e) {
		if (trackingRectangles.size() > 0) {
			ArrayList<Command> commands = new ArrayList<Command>();
			for (TrackingRectangle r : trackingRectangles) {
				if (r.getView().getX() != r.getRectangle().x || 
						r.getView().getY() != r.getRectangle().y) {
					Command c = CommandFactory.createMoveStateCommand(
						r.getView(), r.getRectangle().x, r.getRectangle().y);
					
					if (c != null) {
						commands.add(c);
					}
				}
			}
			if (commands.size() > 0) {
				Command compositeCommand = CommandFactory.
						createCompositeCommand(commands);
				
				graphicalEditor.getViewsManager().executeCommand(
						compositeCommand);
			}
		}
		
		graphicalEditor.setEditorMode(graphicalEditor.getSelectionMode());
	}

	@Override
	public void paintControl(PaintEvent e, Coordinates coordinates) {
		GC gc = e.gc;
		
		for (TrackingRectangle r : trackingRectangles) {
			gc.setForeground(Colors.getBlackColor());
			gc.setBackground(Colors.getGrayColor());
			gc.setAlpha(100);
			Rectangle rect = r.getRectangle();
			gc.fillRectangle(
					coordinates.fromModelXToScreenX(rect.x),
					coordinates.fromModelYToScreenY(rect.y),
					rect.width, rect.height);
			gc.drawRectangle(
					coordinates.fromModelXToScreenX(rect.x),
					coordinates.fromModelYToScreenY(rect.y),
					rect.width, rect.height);
			gc.setAlpha(255);
		}
	}
}
